import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import argparse
import os

def show_result(image_path, output_path, mask_path=None, res='res/trufor/'):
    """
    Visualize the results of an image processing task including input image, 
    ground truth mask (if provided), noiseprint++, localization map, and confidence map.
    
    :param image_path: Path to the input image.
    :param output_path: Path to the output file containing processed results (in .npy format).
    :param mask_path: Optional path to the ground truth mask.
    """
    # Load arguments and results
    result = np.load(output_path)
    image_name = os.path.basename(image_path).split(".")[0]
    # Setup plot
    fig, ax = plt.subplots(figsize=(10, 5))

    def setup_ax(ax, title, image=None, cmap=None, clim=None):
        ax.axis('off')
        if image is not None:
            ax.imshow(image, cmap=cmap, clim=clim)
        ax.set_title(title)
    
    # Image
    setup_ax(ax, None,result['map'], cmap="RdBu_r")
    ax.set_xticks([])
    ax.set_yticks([])
    plt.tight_layout()
    plt.savefig(res+'_local.png', bbox_inches='tight', pad_inches=0)

    setup_ax(ax, None,result['conf'], cmap="gray")
    ax.set_xticks([])
    ax.set_yticks([])
    plt.tight_layout()
    plt.savefig(res+'_conf.png', bbox_inches='tight', pad_inches=0)
    return result['score']

# Example usage of the function with command line arguments simulated
if __name__ == "__main__":
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--image', type=str, help='input image')
    parser.add_argument('--out', type=str, help='output file')
    parser.add_argument('--mask', type=str, default='', help='ground truth mask (optional)')
    parser.add_argument('--result', type=str, help='result image')
    args = parser.parse_args()

    show_result(args.image, args.out, args.mask, args.result)
